﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;

namespace RhotCMS.Hubs
{
    public class HubUserList
    {
        private Dictionary<string, IIdentity> _userDictionary;

        public HubUserList()
        {
            _userDictionary = new Dictionary<string, IIdentity>();
        }

        public IIdentity this[string connectionId]
        {
            get
            {                
                return _userDictionary.SingleOrDefault(x=>x.Key==connectionId).Value;
            }
            set
            {
                if (value == null)
                {
                    _userDictionary.Remove(connectionId);
                    return;
                }


                if (_userDictionary.Keys.SingleOrDefault(x => x == connectionId) != null)
                    return;
                               
                _userDictionary.Add(connectionId, value);
            }
        }

        public IEnumerable<IIdentity> All
        {
            get
            {
                return _userDictionary.Values;
            }
        }

        public int GetUserCountByName(string name)
        {
            return _userDictionary.Values.Select(x => x.Name).Where(x => x == name).Count();
        }
    }
}